package com.bz.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bz.item.service.ItemService;
import com.bz.mapper.TbItemCatMapper;
import com.bz.mapper.TbItemDescMapper;
import com.bz.mapper.TbItemMapper;
import com.bz.mapper.TbItemParamItemMapper;
import com.bz.pojo.TbItem;
import com.bz.pojo.TbItemCat;
import com.bz.pojo.TbItemDesc;
import com.bz.pojo.TbItemParamItem;
import com.bz.utils.PageResult;
import com.bz.vo.ItemVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class ItemServiceImpl extends ServiceImpl<TbItemMapper, TbItem> implements ItemService {

    @Autowired
    private TbItemDescMapper tbItemDescMapper;
    @Autowired
    private TbItemCatMapper tbItemCatMapper;
    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    /**
     * 分页获取商品
     * @param page
     * @param rows
     * @return
     */
    @Override
    public PageResult selectTbItemAllByPage(Integer page, Integer rows) {
        QueryWrapper<TbItem> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbItem::getStatus,1);
        Page<TbItem> pageInfo = PageHelper.startPage(page,rows).doSelectPage(() -> this.baseMapper.selectList(wrapper));
        return new PageResult(pageInfo.getPageNum(),pageInfo.getPages(),pageInfo.getResult());
    }

    /**
     * 新增商品
     * @param tbItem
     * @return
     */
    @Override
    //@LcnTransaction
    public Integer insertTbItem(TbItem tbItem) {
        return this.baseMapper.insert(tbItem);
    }

    /**
     * 更新与删除商品
     * @param tbItem
     * @return
     */
    @Override
    //@LcnTransaction
    public Integer updateItemById(TbItem tbItem) {
        tbItem.setUpdated(new Date());
        return this.baseMapper.updateById(tbItem);
    }

    /**
     * 根据商品ID查询商品，商品分类，商品描述，商品规格参数
     * @param itemId
     * @return
     */
    @Override
    public ItemVo preUpdateItem(Long itemId) {
        ItemVo itemVo = new ItemVo();
        // 根据商品id查询商品
        TbItem item = this.baseMapper.selectById(itemId);
        if (Objects.isNull(item)){
            return null;
        }
        itemVo.setItem(item);
        // 根据商品id查询商品描述
        TbItemDesc itemDesc = tbItemDescMapper.selectById(itemId);
        itemVo.setItemDesc(itemDesc.getItemDesc());
        // 根据商品id查询商品类目
        //QueryWrapper<TbItemCat> itemCatQueryWrapper = new QueryWrapper<>();
        //itemCatQueryWrapper.lambda().eq(item.getCid() != null,TbItemCat::getId,item.getCid());
        //TbItemCat itemCat = tbItemCatMapper.selectOne(itemCatQueryWrapper);
        //itemVo.setItemCat(itemCat);
        if (!Objects.isNull(item.getCid())){
            TbItemCat itemCat = tbItemCatMapper.selectById(item.getCid());
            itemVo.setItemCat(itemCat);
        }
        // 根据商品id查询商品规格
        QueryWrapper<TbItemParamItem> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbItemParamItem::getItemId,itemId);
        TbItemParamItem itemParamItem = tbItemParamItemMapper.selectOne(wrapper);
        itemVo.setItemParamItem(itemParamItem);
        return itemVo;
    }

    /**
     * 根据商品ID查询商品
     * @param itemId
     * @return
     */
    @Override
    public TbItem selectItemInfo(Long itemId) {
        QueryWrapper<TbItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbItem::getStatus,1)
            .eq(TbItem::getId,itemId);
        return this.baseMapper.selectOne(queryWrapper);
    }
}
